<?php
require('common_view_functions.php');

$tag_list_js = '';
foreach($tag_list as $tl) {
	$tag_list_js .= '"' . $tl->tag_text . '",';
}
$tag_list_js = substr($tag_list_js, 1, strlen($tag_list_js) - 3); // need to clear leading and trailing " to stop js errors in some IDE
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo(lang('n8_mgmt_entries_pageTitle')); ?></title>
<link rel="shortcut icon" href="/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/mgmt.css" />
<link rel="stylesheet" type="text/css" href="/css/expressions.css" />
<link rel="stylesheet" type="text/css" href="/css/3rd/jqModal.css" />
</head>
<body id="bodyEntries">
<div id="wrapper">
	<!-- header -->
	<?php require('common_header.php'); ?>
	<!-- /header -->
	<div id="body">
		<div id="side">
			<div id="btnNewEntry" class="sim_button"><a href="<?php echo('/' . $blogger_uri . '/mgmt/edit_entry'); ?>"><?php echo(lang('n8_mgmt_entries_WriteNewEntrySimButtonText')); ?></a></div>
			<div id="entryListTypes">
				<h3 class="sectionHeader"><?php echo(lang('n8_mgmt_entries_sectionHeaderEntryList')); ?></h3>
				<dl>
					<dt><a rel="Group-0099" href="javascript:;"><?php echo(lang('n8_mgmt_entries_listEntryByStatusTitle')); ?></a></dt>
					<dd id="group_0099">
						<ul>
							<li><a rel="List-0099-00" href="javascript:;"><?php echo(lang('n8_mgmt_entries_listEntryUnpublished')); ?> ( <?php echo($draft_num); ?> )</a></li>
						</ul>
					</dd>
					<?php
					$pyr = 0;
					$lyr = $current_visiting_month[0];
					foreach($month_list as $month) {
						$yr = $month->year;
						if($yr == $pyr) {
							continue;
						} else {
					?>
					<dt><a rel="Group-<?php echo($yr); ?>" href="javascript:;"><b><?php echo($yr); ?></b></a></dt>
					<dd id="group_<?php echo($yr); ?>"<?php if($lyr != $yr) { ?> style="display:none;"<?php } ?>>
						<ul>
							<?php
							foreach($month_list as $m) {
								if($m->year == $yr) {
							?>
							<li><a rel="List-<?php echo($m->year); ?>-<?php echo($m->month); ?>" href="javascript:;"><?php echo($m->year . ' / ' . $m->month); ?> ( <span class="entryCount"><?php echo($m->num); ?></span> )</a></li>
							<?php
								}
							}
							?>
						</ul>
					</dd>
					<?php
						}
						$pyr = $yr;
					}
					?>
				</dl>
			</div>
			<!--
			<div id="btnImport" class="sim_button"><a onclick="javascript:alert('Not implemented yet!');return false;" href="<?php echo('/' . $blogger_uri . '/mgmt/import'); ?>">网志导入及聚合</a></div>-->
		</div>
		<div id="main">
			<div id="entryListContainer">Loading...</div>
			<fieldset id="batchProcs">
				<legend><?php echo(lang('n8_mgmt_entries_batchProcFieldTitle')); ?></legend>
				<form id="frmBatchProc" name="frmBatchProc" action="">
					<p>
						<select id="selChangeCate" name="selChangeCate" class="textBox">
							<option value="-1" selected="selected"><?php echo(lang('n8_mgmt_entries_changeCateOptionTitle')); ?></option>
							<option value="1"><?php echo(lang('n8_mgmt_entries_defaultCategoryTitle')); ?></option>
							<?php foreach($cust_cates as $cc) { ?>
							<option value="<?php echo($cc->id); ?>"><?php echo(htmlspec($cc->title)); ?></option>
							<?php } ?>
						</select>
						|
						<select id="selChangeState" name="selChangeState" class="textBox">
							<option value="-1" selected="selected"><?php echo(lang('n8_mgmt_entries_changeStateOptionTitle')); ?></option>
							<option value="0"><?php echo(lang('n8_mgmt_entries_stateSecret')); ?></option>
							<option value="1"><?php echo(lang('n8_mgmt_entries_stateNormal')); ?></option>
							<option value="3"><?php echo(lang('n8_mgmt_entries_stateInternal')); ?></option>
						</select>
						|
						<input type="button" id="btnBatchDelete" name="btnBatchDelete" class="button" value="<?php echo(lang('n8_mgmt_entries_batchDeleteButtonValue')); ?>" />
					</p>
					<p>
						<input type="text" id="txtBatchTags" name="txtBatchTags" class="textBox" size="16" style="width:16em;" /> <input type="button" id="btnBatchSetTags" name="btnBatchSetTags" class="button" value="<?php echo(lang('n8_mgmt_entries_batchSetTagsButtonValue')); ?>" />
						<div id="autosuggest"><ul></ul></div>
					</p>
				</form>
			</fieldset>
		</div>
		<div class="z"></div>
	</div>
	<!-- footer -->
	<?php require('common_footer.php'); ?>
	<!-- /footer -->
</div>

<!-- JS -->
<script type="text/javascript" src="/js/3rd/jquery.pack.js"></script>
<script type="text/javascript" src="/js/3rd/jqModal.js"></script>
<script type="text/javascript" src="/js/3rd/json2.js"></script>
<script type="text/javascript" src="/js/3rd/auto_suggest.js"></script>
<script type="text/javascript">
<!--
var g_userSiteRoot = "<?php echo('/' . $blogger_uri); ?>";

var entryListLoaded = false;
var currentEntryListGroup = "List-<?php echo($current_visiting_month[0]); ?>-<?php echo($current_visiting_month[1]); ?>";
var batchEntryTagsString = "";
var deletedId = 0;
var deletedCount = 0;

function refreshCurrentEntryList() {
	if(currentEntryListGroup != "") {
		gt = currentEntryListGroup.split("-");
		loadEntryListPage(gt[1], gt[2]);
	}
}

function getCheckedEntries() {
	var ids = $("#tblEntryList td.chk input:checked");
	var idsarr = [];
	if(ids.length > 0) {
		for(var i = 0; i < ids.length; i++) {
			idsarr.push(ids[i].value);
		}
	}
	return idsarr;
}

function getCheckedEntriesAsString() {
	var ids = $("#tblEntryList td.chk input:checked");
	var idsstr = "";
	if(ids.length > 0) {
		var idsarr = [];
		for(var i = 0; i < ids.length; i++) {
			idsarr.push(ids[i].value);
		}
		idsstr = idsarr.join(", ");
	}
	return idsstr;
}

function purgeTagString(ts) {
	var ps = jQuery.trim(ts);
	ps = ps.replace(/\s{2,}/ig, " ");
	return ps;
}

function validateTags() {
	var batchTags = $("#txtBatchTags").val();
	if(batchTags != "") {
		if(/[^\u4E00-\u9FA0a-zA-Z0-9._ ]/.test(batchTags)) {
			alert("Tag 中不能含有除中文、字母、数字、句点及下划线之外的其他字符！");
			return false;
		}
		
		batchTags = purgeTagString(batchTags);
		var hasSame = false;
		var arrTags = batchTags.split(" ");
		for(var i = 0; i < arrTags.length; i++) {
			for(var j = i + 1; j < arrTags.length; j++) {
				if(arrTags[i] == arrTags[j]) {
					hasSame = true;
					break;
				}
			}
		}
		if(hasSame) {
			alert("Tag 有重复，请修改！");
			return false;
		}
		batchEntryTagsString = batchTags;
	}
	return true;
}

function changeCateCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	$("#selChangeCate").val("-1"); // restore
	$("#selChangeCate").blur();
	refreshCurrentEntryList();
}

function changeStateCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	$("#selChangeState").val("-1");
	$("#selChangeState").blur();
	refreshCurrentEntryList();
}

function batchDeleteCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	refreshCurrentEntryList();
	var monthItem = $("#entryListTypes dl dd ul li a[rel=" + currentEntryListGroup + "]").find("span.entryCount");
	var entryCount = parseInt(monthItem.text()) - deletedCount;
	monthItem.text(entryCount.toString());
}

function batchSetTagsCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed");
		return;
	}
	
	batchEntryTagsString = "";
	$("#txtBatchTags").val("");
	refreshCurrentEntryList();
}

function deleteEntryCallback(rdata) {
	$("#ajaxLoading").hide();
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	$("#entryRow" + deletedId).css("background-color", "#ccc").fadeOut("slow");
	var monthItem = $("#entryListTypes dl dd ul li a[rel=" + currentEntryListGroup + "]").find("span.entryCount");
	var entryCount = parseInt(monthItem.text()) - 1;
	monthItem.text(entryCount.toString());
}

function loadEntryListDone() {
	$("#ajaxLoading").hide();
	
	$("#tblEntryList td.oper a").click(function() {
		var rid = $(this).attr("rel");
		rid = rid.split("_");
		if(rid[0] == "d") {
			if(confirm("Are you sure to delete this entry?")) {
				// do the delete ajaxy
				$("#ajaxLoading").show();
				deletedId = rid[1];
				$.post(g_userSiteRoot + "/mgmt/entries", {
					"hidIsPostBack": "ja",
					"operation": "delete_entry",
					"entry_id": deletedId
				}, deleteEntryCallback);
			}
		}
	});
	
	entryListLoaded = true;
}

function loadEntryListPage(year_string, month_string) {
	if(year_string == "0" || month_string == "0") {
		$("#entryListContainer").html("");
		return;
	}
	$("#ajaxLoading").show();
	$("#entryListContainer").load(g_userSiteRoot + "/mgmt/entries_list/" + year_string + "/" + month_string, {
		"R": Math.random()
	}, loadEntryListDone);
}

var myTags = new Array("<?php echo($tag_list_js); ?>");

$(document).ready(function() {
	$("#entryListTypes dl dt a").click(function() {
		var gtext = $(this).attr("rel");
		gtext = gtext.split("-");
		var list_dd = $("#entryListTypes dl dd#group_" + gtext[1]);
		list_dd.toggle();
	});
	
	$("#entryListTypes dl dd ul li a").click(function() {
		var gtext = $(this).attr("rel");
		currentEntryListGroup = gtext;
		gtext = gtext.split("-");
		loadEntryListPage(gtext[1], gtext[2]);
	});
	
	$("#selChangeCate").change(function() {
		if(!entryListLoaded) {
			return;
		}
		
		var newCateId = $(this).val();
		if(newCateId == "-1") {
			return;
		}
		
		var arrIds = getCheckedEntries();
		if(arrIds.length == 0) {
			alert("没有选择任何网志！");
			$(this).val("-1");
			return;
		}
		
		if(newCateId != "-1") {
			if(confirm("确实要改变所选网志的分类归属？")) {
				$("#ajaxLoading").show();
				$.post(g_userSiteRoot + "/mgmt/entries", {
					"hidIsPostBack": "ja",
					"operation": "batch_change_category",
					"new_cate_id": newCateId,
					"posted_data": JSON.stringify(arrIds)
				}, changeCateCallback);
			}
		}
	});
	
	$("#selChangeState").change(function() {
		if(!entryListLoaded) {
			return;
		}
		
		var newState = $(this).val();
		if(newState == "-1") {
			return;
		}
		
		var arrIds = getCheckedEntries();
		if(arrIds.length == 0) {
			alert("没有选择任何网志！");
			$(this).val("-1");
			return;
		}
		
		if(newState != "-1") {
			if(confirm("确实要改变所选网志的状态？")) {
				$("#ajaxLoading").show();
				$.post(g_userSiteRoot + "/mgmt/entries", {
					"hidIsPostBack": "ja",
					"operation": "batch_change_state",
					"new_state": newState,
					"posted_data": JSON.stringify(arrIds)
				}, changeStateCallback);
			}
		}
	});
	
	$("#btnBatchDelete").click(function() {
		if(!entryListLoaded) {
			return;
		}
		
		var arrIds = getCheckedEntries();
		if(arrIds.length == 0) {
			alert("没有选择任何网志！");
			return;
		}
		
		if(confirm("确定要删除所选网志？")) {
			$("#ajaxLoading").show();
			deletedCount = arrIds.length;
			$.post(g_userSiteRoot + "/mgmt/entries", {
				"hidIsPostBack": "ja",
				"operation": "batch_delete",
				"posted_data": JSON.stringify(arrIds)
			}, batchDeleteCallback);
		}
	});
	
	$("#btnBatchSetTags").click(function() {
		if(!entryListLoaded) {
			return;
		}
		
		var arrIds = getCheckedEntries();
		if(arrIds.length == 0) {
			alert("没有选择任何网志！");
			return;
		}
		
		if(confirm("确实要修改所选网志的 Tag？")) {
			if(validateTags()) {
				$("#ajaxLoading").show();
				$.post(g_userSiteRoot + "/mgmt/entries", {
					"hidIsPostBack": "ja",
					"operation": "batch_set_tags",
					"new_tags": batchEntryTagsString,
					"posted_data": JSON.stringify(arrIds)
				}, batchSetTagsCallback);
			}
		}
	});
	
	new AutoSuggest(document.getElementById("txtBatchTags"), myTags);
	
	loadEntryListPage("<?php echo($current_visiting_month[0]); ?>", "<?php echo($current_visiting_month[1]); ?>");
});
// -->
</script>
<!-- /JS -->

<div id="ajaxLoading"><img alt="Loading..." src="/img/ajax_loading.gif" border="0" width="24" height="24" /></div>
</body>
</html>
